package me.kagami.myplugin.extensions;

import com.intellij.icons.AllIcons;
import com.intellij.ide.SelectInTarget;
import com.intellij.ide.impl.ProjectViewSelectInTarget;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.AbstractProjectViewPSIPane;
import com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase;
import com.intellij.ide.projectView.impl.ProjectTreeStructure;
import com.intellij.ide.projectView.impl.ProjectViewTree;
import com.intellij.ide.util.treeView.AbstractTreeBuilder;
import com.intellij.ide.util.treeView.AbstractTreeUpdater;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;

public class CustomizeProjectViewPane extends AbstractProjectViewPSIPane {

    public static final String ID = "CUSTOMIZE";

    protected CustomizeProjectViewPane(@NotNull Project project) {
        super(project);
    }

    @Override
    protected @NotNull
    ProjectAbstractTreeStructureBase createStructure() {
        ProjectTreeStructure projectTreeStructure = new ProjectTreeStructure(myProject, ID) {
            @Override
            protected CustomizeProjectNode createRoot(@NotNull Project project, @NotNull ViewSettings settings) {
                return new CustomizeProjectNode(project);
            }

            // Children will be searched in async mode
            @Override
            public boolean isToBuildChildrenInBackground(@NotNull Object element) {
                return true;
            }
        };
        return projectTreeStructure;
    }

    @Override
    protected @NotNull
    ProjectViewTree createTree(@NotNull DefaultTreeModel defaultTreeModel) {
        return new ProjectViewTree(defaultTreeModel) {
            @Override
            public boolean isRootVisible() {
                return true;
            }
        };
    }

    @Override
    protected @NotNull
    AbstractTreeUpdater createTreeUpdater(@NotNull AbstractTreeBuilder abstractTreeBuilder) {
        return new CustomizeTreeUpdater(abstractTreeBuilder);
    }

    @Override
    public @NotNull
    @Nls(capitalization = Nls.Capitalization.Title)
    String getTitle() {
        return "自定义视图";
    }

    @Override
    public @NotNull
    Icon getIcon() {
        return AllIcons.FileTypes.Custom;
    }

    @Override
    public @NotNull
    String getId() {
        return ID;
    }

    @Override
    public int getWeight() {
        return 10;
    }

    @Override
    public @NotNull
    SelectInTarget createSelectInTarget() {
        return new ProjectViewSelectInTarget(myProject) {

            @Override
            public String toString() {
                return ID;
            }

            @Override
            public String getMinorViewId() {
                return ID;
            }

            @Override
            public float getWeight() {
                return 10;
            }
        };
    }
}
